package com.zw.transition.controller;


import com.zw.transition.base.BaseController;
import com.zw.transition.common.JsonResult;
import com.zw.transition.entity.*;
import com.zw.transition.services.AssemblyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/assembly")
public class AssemblyController extends BaseController {

    @Autowired
    private AssemblyService assemblyService;

    /**
     * 申请，接收页面
     */
    @GetMapping("/{type}/{code}")
    public String scan(@PathVariable("type") Integer type, @PathVariable("code") String code, Model model) {
        model.addAttribute("url", "/scan/" + type);
        if (code != null) {
            code = code.replace("◐", "/");
            code = code.replace("◑", "/");
        }
        AssemblySheet sheet = assemblyService.findSheet(code);
        if (sheet == null) {
            return "assembly/blank";
        }
        model.addAttribute("sheet", assemblyService.findSheet(code));
        if (type == 1) {
            // 申请质检
            List<AssemblyRoute> list = assemblyService.findApplyList(code);
            List<AssemblyRoute> completeList = assemblyService.findCompleteApplyList1(code);
            model.addAttribute("list", list);
            model.addAttribute("completeList", completeList);
            return "assembly/apply";
        } else if (type == 2) {
            // 质检
            List<AssemblyModel> list = assemblyService.findCheckList(code);
            List<AssemblyRoute> completeList = assemblyService.findCompleteApplyList2(code);
            model.addAttribute("list", list);
            model.addAttribute("completeList", completeList);
            return "assembly/check";
        } else if (type == 3) {
            // 转序接收
            List<AssemblyModel> list = assemblyService.findRecList(code);
            List<AssemblyRoute> completeList = assemblyService.findCompleteApplyList(code);
            model.addAttribute("list", list);
            model.addAttribute("completeList", completeList);
            return "assembly/rec";
        }
        return "error";
    }

    /**
     * 申请质检
     */
    @ResponseBody
    @PostMapping("/apply")
    public JsonResult apply(AssemblyRoute route) {
        return assemblyService.apply(route);
    }

    /**
     * 跳转到历史申请
     */
    @GetMapping("/hisApply")
    public String hisApply(Model model) {
        model.addAttribute("hisApply", assemblyService.findHisApplyList());
        return "assembly/historicalApply";
    }

    /**
     * 撤销申请
     */
    @ResponseBody
    @PostMapping("/cancel/apply")
    public JsonResult cancelApply(AssemblyTransSheet trans) {
        // 转序单已被删除
        if (trans.getIsDel() == 1) {
            return error("此转序已删除无法撤销申请！");
        }
        // 如果已经被质检
        AssemblyTransCheck transCheck = assemblyService.getTransSheetCheck(trans.getId());
        if (transCheck.getStatus() != 1 && transCheck.getStatus() != 4) {
            return error("此转序已质检无法撤销申请！");
        }
        //当前路线
        return jsonResult(assemblyService.cancelApply(trans.getId()));
    }

    /**
     * 删除
     */
    @ResponseBody
    @PostMapping("/delApply")
    public JsonResult delApply(Integer id) {
        return jsonResult(assemblyService.delApply(id));
    }

    /**
     * 重新申请
     */
    @ResponseBody
    @PostMapping("/repeatApply")
    public JsonResult repeatApply(Integer id) {
        return jsonResult(assemblyService.repeatApply(id));
    }

    /**
     * 质检驳回
     */
    @ResponseBody
    @PostMapping("/reject")
    public JsonResult reject(FlowTrans trans) {
        return jsonResult(assemblyService.reject(trans));
    }

    /**
     * 跳转到历史质检
     */
    @GetMapping("/hisCheck")
    public String hisCheck(Model model) {
        model.addAttribute("hisCheck", assemblyService.findHisCheckList());
        return "assembly/historicalCheck";
    }

    /**
     * 撤销质检
     */
    @ResponseBody
    @PostMapping("/cancel/check")
    public JsonResult cancelCheck(AssemblyTransSheet trans) {
        return jsonResult(assemblyService.cancelCheck(trans.getId()));
    }

    /**
     * 质检合格
     */
    @ResponseBody
    @PostMapping("/success")
    public JsonResult success(FlowTrans trans) {
        return jsonResult(assemblyService.success(trans));
    }

    /**
     * 接收
     */
    @ResponseBody
    @PostMapping("/rec")
    public JsonResult rec(FlowTrans trans) {
        return jsonResult(assemblyService.rec(trans));
    }

    /**
     * 跳转到历史接收
     */
    @GetMapping("/hisRec")
    public String hisRec(Model model) {
        model.addAttribute("hisRec", assemblyService.findHisRecList());
        return "assembly/historicalRec";
    }

    /**
     * 撤销接收
     */
    @ResponseBody
    @PostMapping("/cancel/rec")
    public JsonResult cancelRec(FlowTrans trans) {
        return jsonResult(assemblyService.cancelRec(trans));
    }

}
